package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Inventory;
import com.ruoyi.system.domain.MaterialSix;
import com.ruoyi.system.domain.MaterialTwo;
import com.ruoyi.system.domain.PickCondition;
import com.ruoyi.system.mapper.WarehouseDetailMapper;
import com.ruoyi.system.service.IWarehouseDetailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseDetalServiceImpl implements IWarehouseDetailService {
    @Resource
    public WarehouseDetailMapper warehouseDetailMapper;

    public List<MaterialSix> selectMaterialList(PickCondition pickCondition, Integer wid, Integer mid) {
        HashMap<String,Object> map= new HashMap<String, Object>();
        map.put("wid",wid);
        map.put("mid",mid);
        map.put("pickCondition",pickCondition);
        List<MaterialSix> list=warehouseDetailMapper.selectMaterialList(map);
        for (int i = 0; i <list.size() ; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(list.get(i).getMdPrice()));
            BigDecimal b2 = new BigDecimal(Double.toString(list.get(i).getMdSum()));
            DecimalFormat df = new DecimalFormat("######0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);

            list.get(i).setMdMoney(Double.valueOf(df.format(new BigDecimal(b1.multiply(b2).doubleValue()+""))));
        }
        return list;
    }
    public List<MaterialTwo> selectMaterialRecordList(PickCondition pickCondition, Integer wid, Integer mid) {
        HashMap<String,Object> map= new HashMap<String, Object>();
        map.put("wid",wid);
        map.put("mid",mid);
        map.put("pickCondition",pickCondition);
        List<MaterialTwo> list=warehouseDetailMapper.selectMaterialRecordList(map);
        for (int i = 0; i <list.size() ; i++) {
            BigDecimal b1 = new BigDecimal(Double.toString(list.get(i).getMdPrice()));
            BigDecimal b2 = new BigDecimal(Double.toString(list.get(i).getWrNumber()));
            DecimalFormat df = new DecimalFormat("######0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            list.get(i).setMdMoney(Double.valueOf(df.format(new BigDecimal(b1.multiply(b2).doubleValue()+""))));
        }
        return list;
    }

    //盘点接口
    @Override
    public boolean updateInventory(Inventory inventory) {
        int month = inventory.getDate().getMonth()+1;
        Long aLong = inventory.getmId();
        HashMap<String,Object> map=new HashMap<>();
        map.put("month",month);
        map.put("mId",aLong);
        map.put("wId",inventory.getwId());
        return (warehouseDetailMapper.deleteInventory(map)>=0)&&(warehouseDetailMapper.insertInventory(inventory)>0);
    }

    @Override
    public int deleteMateriaDetaill(Long mdId) {
        int  a=warehouseDetailMapper.deleteMaterialdetails(mdId);
        if(a>0){
            a+=warehouseDetailMapper.deletewarehouserecord(mdId);
            return a;
        }else {
            return 0;
        }

    }

    @Override
    public int updateWarehouseMateriaDetail(MaterialSix materialSix) {

        return warehouseDetailMapper.updateWarehouseMateriaDetail(materialSix);
    }

    @Override
    public List<Map> queryMaterial(int type,String mModel, String mName,  String mUnit, String proName ,String proNumber, String wName) {
        HashMap<String,Object> map=new HashMap();
        map.put("type",type);
        map.put("mName",mName);
        map.put("mModel",mModel);
        map.put("mUnit",mUnit);
        map.put("proNumber",proNumber);
        map.put("proName",proName);
        map.put("wName",wName);
        System.out.println(map);
        return warehouseDetailMapper.queryMaterial(map);
    }

}
